package testing;

import java.awt.Color;
import java.awt.Graphics2D;
//import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import dse.Cluster;
import dse.Node;
//import dse.Pair;
import dse.modules.eln.EarthLikeNetwork;
import dse.modules.eln.NodeWithCoordinates;

/**
 * This is a static utility class with methods for creating visual images from
 * some of the program data types.  It is pretty much for debugging :)
 * 
 * @author Aaron
 */
public class EarthLikeNetworkImagingUtil {
	private final static double ImageWidth = 1000;
	private final static double ImageHeight = 500;
	private final static double Radius = 250;
	
	/** This is a utility class, so no instances are allowed. */
	private EarthLikeNetworkImagingUtil () {/**/}
	
	/**
	 * Draws a network and returns it.
	 * @param network
	 * @return
	 */
	public static BufferedImage drawNetwork (
			EarthLikeNetwork network
			) 
	{
		BufferedImage output = new BufferedImage(
				(int)ImageWidth, (int)ImageHeight, BufferedImage.TYPE_INT_ARGB
				);
		Graphics2D g = output.createGraphics();
		
		g.fillOval(0, 0, 2*(int)Radius, 2*(int)Radius);
		g.fillOval(2*(int)Radius, 0, 2*(int)Radius, 2*(int)Radius);
		//g.fillRect(0,0,1000,1000);
		g.setColor(java.awt.Color.RED);
		g.drawLine(0, 250, 1000, 250);
		g.setColor(java.awt.Color.BLUE);
		for (Node n : network.nodes()) {
			NodeWithCoordinates nwc = (NodeWithCoordinates)n;
			if(nwc.getX() >= 0)
			{
				double x = Radius*(1-Math.cos(nwc.getY()*Math.PI/180)* 
						Math.cos(nwc.getX()*Math.PI/180));
				double y = Radius*(1-Math.sin(nwc.getY()*Math.PI/180));
				g.fillRect((int)x, (int)y, 2, 2);
			}
			else
			{
				double x = Radius*(3-Math.cos(nwc.getY()*Math.PI/180)*
						Math.cos((nwc.getX()+180)*Math.PI/180));
				double y = Radius*(1-Math.sin(nwc.getY()*Math.PI/180));
				g.fillRect((int)x, (int)y, 2, 2);
			}
		}
		
		return output;
	}
		
	/**
	 * Draws a network and saves it to a file.
	 * @param network
	 * @param filename
	 * @throws IOException
	 */
	public static void drawNetwork (
			EarthLikeNetwork network, 
			String filename
			) 
	throws IOException {
		saveImage(drawNetwork(network), filename);
	}

	/**
	 * This draws the clusters of a network onto an existing image (created with
	 * drawNetwork most likely).  It then returns the same image passed to it.
	 */
	public static BufferedImage drawClusters (
			BufferedImage image,
			//int networkWidth, int networkHeight,
			//Iterable<Pair<Integer,Rectangle>> clusterAreas
			EarthLikeNetwork network 
			) 
	{
		Random rand = new Random ();
		Graphics2D g = image.createGraphics();
		
		//double wdiff = (networkWidth/ImageWidth);
		//double hdiff = (networkHeight/ImageHeight);
		
		for (Cluster c : network.clusters()) 
		{
			int id = c.id();
			
			int red = rand.nextInt(255);
			int green = rand.nextInt(255);
			int blue = rand.nextInt(255);
			
			g.setColor(new Color (red, green, blue, 90));
			for(Node n: c.nodes())
			{
				NodeWithCoordinates nwc = (NodeWithCoordinates)n;
				double x = 0;
				double y = 0;
				if(nwc.getX() >= 0)
				{
					x = Radius*(1-Math.cos(nwc.getY()*Math.PI/180)* 
							Math.cos(nwc.getX()*Math.PI/180));
					y = Radius*(1-Math.sin(nwc.getY()*Math.PI/180));
					g.fillRect((int)x, (int)y, 2, 2	);
					
				}
				else
				{
					x = Radius*(3-Math.cos(nwc.getY()*Math.PI/180)*
							Math.cos((nwc.getX()+180)*Math.PI/180));
					y = Radius*(1-Math.sin(nwc.getY()*Math.PI/180));
					g.fillRect((int)x, (int)y, 2, 2);
				}
				
				if(nwc.isSupernode())
				{
					g.setColor(Color.BLACK);
					g.drawString(""+id, (int)x, (int)y);
					g.setColor(new Color (red, green, blue, 90));
				}
			}
		}
		return image;
	}
	
	/**
	 * This is a real helper method to save an image.  I can never remember how
	 * you are supposed to do this in Java ;)
	 * @param image
	 * @param filename
	 * @throws IOException
	 */
	public static void saveImage (BufferedImage image, String filename)
	throws IOException
	{
		ImageIO.write(image, "PNG", new File (filename));
	}
}
